package com.kucai.yifaqapp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.dagger.component.activity.DaggerLongsidesActivityComponent;
import com.kucai.yifaqapp.dagger.module.activity.LongsidesActivityModule;
import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.model.network.entity.res.ResLong;
import com.kucai.yifaqapp.presenter.activity.LongsidesActivityPesenter;
import com.kucai.yifaqapp.ui.view.LongsidesView;
import com.kucai.yifaqapp.util.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import static com.kucai.yifaqapp.global.App.getContext;

public class LongsidesActivity extends BaseActivity<LongsidesActivityPesenter> implements LongsidesView {

    TextView titleLeftTv;
    TextView headerTitle;
    RecyclerView longsidesRl;
    LinearLayout skinbg;
    private Adapter adapter;
    private List<ResLong.LineRightBean> list = new ArrayList<>();//存放所有赔率的集合
    private String gameid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleLeftTv = findViewById(R.id.title_left_tv);
        headerTitle = findViewById(R.id.headerTitle);
        longsidesRl = findViewById(R.id.longsides_rl);
        skinbg = findViewById(R.id.skinbg);


        Intent intent = getIntent();
        gameid = intent.getStringExtra("gameid");
        if (gameid != null) {
            String a = HttpAdress.getuseAdress() + "mobile/static/data/clong/";
            String b = gameid + ".json";
            mPresenter.getlong(a + b);
        }
        titleLeftTv.setText("返回");
        titleLeftTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        headerTitle.setText("两面长龙");
        longsidesRl.setLayoutManager(new LinearLayoutManager(this));
        //创建适配器
        adapter = new Adapter();
        longsidesRl.setAdapter(adapter);
    }

    @Override
    protected void initDagger() {
        DaggerLongsidesActivityComponent.builder()
                .longsidesActivityModule(new LongsidesActivityModule(this))
                .build().inject(this);
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_longsides;
    }

    @Override
    protected void ChangeSkin() {
        switch (SkinId) {
            case 17771:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
                break;
            case 17772:
                skinbg.setBackgroundResource(R.mipmap.skin_bg2);
                break;
            case 17773:
                skinbg.setBackgroundResource(R.mipmap.skin_bg3);
                break;
            default:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
        }
    }

    @Override
    public void getlog(ResLong data) {
        list.clear();
        list.addAll(data.line_right);
        adapter.notifyDataSetChanged();
    }

    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        @Override
        public MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.longsides_item, parent, false));

        }

        @Override
        public void onBindViewHolder(final MViewHolder holder, final int position) {
            if (null != list.get(position).chname) {
                holder.longname.setText(list.get(position).chname);
            }
            if (0 != list.get(position).num) {
                holder.longnum.setText(list.get(position).num + "");
            }

        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView longname, longnum;

            MViewHolder(View itemView) {
                super(itemView);
                longname = (TextView) itemView.findViewById(R.id.longname);
                longnum = (TextView) itemView.findViewById(R.id.longnum);


            }
        }
    }
}
